﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Schema;
using PNProject.Core;
using PNProject.Core.Converter;
using PNProject.EF;

namespace PNProject.Controllers
{
    [Authorize]
    public class AdminProductController : Controller
    {
        //
        // GET: /AdminSupport/

        public ActionResult Index()
        {
            var products = PublicService.GetAllItems((int)EnumType.ItemType.Product);
            return View(products);
        }


        public ActionResult Create()
        {
            return View(new ItemModel());
        }

        [HttpPost]
        public ActionResult Create(ItemModel model, List<HttpPostedFileBase> avatar)
        {
           
            bool legal = true;
            if (string.IsNullOrEmpty(model.Url))
            {
                //luu file
                if (avatar != null)
                {
                    model.Images=new List<ImagesItem>();
                    foreach (var ava in avatar)
                    {
                        legal = ava.ContentType == "image/jpeg" || ava.ContentType == "image/png" || ava.ContentType == "image/gif" || ava.ContentType == "image/jpg";
                        if (legal)
                        {
                            string time = PNLibraries.PNConvertToTimestamp(DateTime.Now).ToString();
                            string newName = time + "-" + ava.FileName;
                            string path = "/Application_Data/Images/";
                            ServerHelper.SaveFileBaseOnServer(path, ava, newName);
                            var image = new ImagesItem();
                            image.Url = path + newName;
                            
                            model.Images.Add(image);
                        }
                    }
                    
                }
            }
            model.CreatedDate = DateTime.Now;
            model.ModifiedDate = DateTime.Now;
            model.ItemType = (int)EnumType.ItemType.Product;
            model.ItemId = Guid.NewGuid();
            model.Status = true;
            var obj = ItemConverter.ToItem(model);
            if ((avatar != null || !string.IsNullOrEmpty(model.Url)) && legal)
                StaticData.itemRep.Save(null, obj);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            try
            {
                Guid newid = Guid.Parse(id);
                var dbo = StaticData.itemRep.GetOne(a => a.ItemID == newid);
                var model = ItemConverter.ItemTo(dbo);
                return View(model);
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(ItemModel model)
        {
            try
            {
                Guid newsid = Guid.Parse(model.ItemId.ToString());
                var obj = StaticData.itemRep.GetOne(a => a.ItemID == newsid);
                var newobj = ItemConverter.ToItem(model);
                newobj.ItemID = obj.ItemID;
                StaticData.itemRep.Save(obj, newobj);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }

        }
        /// <summary>
        /// delete update status
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            try
            {
                var newid = Guid.Parse(id);
                var dbo = StaticData.itemRep.GetOne(a => a.ItemID == newid);
                var modify = ItemConverter.CopyFeedback(dbo);
                modify.IsActive = false;
                StaticData.itemRep.Save(dbo, modify);
            }
            catch (Exception e)
            {

            }
            return RedirectToAction("Index");
        }
    }
}
